import { MathUitl } from "../../../../../com/gengine/utils/MathUitl";
import { DiceItem } from "./DiceItem";
import { DiceRuningType } from "./DiceRuningType";
import { IDiceBar } from "./interfaces/IDiceBar";
import { IDiceHand } from "./interfaces/IDiceHand";
type int = number;
//class DicesUtil
    
    export  class DicesUtil /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._diceData = this.DefaultDiceData;
            return;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this.removeEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            this.registEventListener(true);
            return;
        }

        protected registEventListener(arg1: Boolean): void
        {
            if (!this._moveingArea) 
            {
                return;
            }
            if (arg1) 
            {
                this._moveingArea.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler);
                this._moveingArea.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.onMouseOutHandler);
                this._moveingArea.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.onMouseOverHandler);
                this._moveingArea.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDownHandler);
            }
            else 
            {
                this._moveingArea.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler);
                this._moveingArea.removeEventListener(flash.events.MouseEvent.ROLL_OUT, this.onMouseOutHandler);
                this._moveingArea.removeEventListener(flash.events.MouseEvent.ROLL_OVER, this.onMouseOverHandler);
                this._moveingArea.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDownHandler);
            }
            return;
        }

        /* internal  */onRemoveFromStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            this.removeEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            this.registEventListener(false);
            return;
        }

        protected onMouseMoveHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._isShowHand) 
            {
                if (this._diceHand) 
                {
                    this._diceHand.x = mouseX;
                    this._diceHand.y = mouseY;
                }
            }
            return;
        }

        protected onMouseOutHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._isShowHand) 
            {
                this._isShowHand = false;
                if (this._diceHand) 
                {
                    flash.ui.Mouse.show();
                    this._diceHand.hide();
                }
            }
            return;
        }

        protected onMouseDownHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._isRunning || this._isShowHand == false) 
            {
                return;
            }
            this.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.onMouseUpHandler);
            this._timer.addEventListener(flash.events.TimerEvent.TIMER, this.onTimerHandler);
            this._timer.start();
            this.power = 0;
            if (this._powerbar) 
            {
                this._powerbar.show(this);
            }
            if (this._diceHand) 
            {
                this._diceHand.mouseDown();
            }
            return;
        }

        protected onMouseOverHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._isShowHand == false) 
            {
                this._isShowHand = true;
                if (this._diceHand) 
                {
                    flash.ui.Mouse.hide();
                    this._diceHand.show(this);
                }
                this.onMouseMoveHandler(null);
            }
            return;
        }

        protected onMouseUpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.onMouseUpHandler);
            this._timer.removeEventListener(flash.events.TimerEvent.TIMER, this.onTimerHandler);
            this._timer.stop();
            this.onClickHandler(null);
            if (this._powerbar) 
            {
                this._powerbar.hide();
            }
            if (this._diceHand) 
            {
                this._diceHand.mouseUp();
            }
            return;
        }

        /* internal  */onTimerHandler(arg1: Object /* flash.events.TimerEvent */): void
        {
            if (this.power > 100) 
            {
                this.isUp = false;
            }
            else if (this.power <= 0) 
            {
                this.isUp = true;
            }
            if (this.isUp) 
            {
                this.power = this.power + 5;
            }
            else 
            {
                this.power = this.power - 5;
            }
            return;
        }

        /* internal  */onClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (typeof this._startPlayFunction === "function") 
            {
                this._startPlayFunction();
            }
            return;
        }

        public set diceData(arg1: Array<any>)
        {
            if (!(arg1 == null) && arg1.length > 0) 
            {
                this._diceData = arg1;
            }
            else 
            {
                this._diceData = null;
            }
            return;
        }

        public get power(): int
        {
            return this._power;
        }

        public set power(arg1: int)
        {
            this._power = arg1;
            if (this._powerbar) 
            {
                this._powerbar.gotoAndStop(this._power);
                this._powerbar.label = this._power + "%";
            }
            return;
        }

        protected getDiceDropDownFromY(arg1: int): number
        {
            return (-(this.power + 20)) * 4;
        }

        protected getRondomPoints(): Array<any>
        {
            var loc1=0;
            if (this._tempPoints == null) 
            {
                this._tempPoints = [];
                loc1 = 0;
                while (loc1 < this._diceNum) 
                {
                    this._tempPoints.push(new Object /* flash.geom.Point */());
                    ++loc1;
                }
            }
            loc1 = 0;
            while (loc1 < this._diceNum) 
            {
                this._tempPoints[loc1].x = 0;
                ++loc1;
            }
            loc1 = 0;
            while (loc1 < this._diceNum) 
            {
                this._tempPoints[loc1].y = MathUitl.random(-2, 2) * this._diceItemH;
                ++loc1;
            }
            return this._tempPoints;
        }

        /* internal  */clearDice(): void
        {
            var loc1=0;
            if (this.diceAry && this.diceAry.length > 0) 
            {
                loc1 = 0;
                while (loc1 < this.diceAry.length) 
                {
                    this._dice = this.diceAry[loc1] as DiceItem;
                    if (this._dice && this._dice.parent) 
                    {
                        this._dice.parent.removeChild(this._dice);
                    }
                    ++loc1;
                }
            }
            return;
        }

        /* internal  */rondomDice(arg1: int): void
        {
            if (this._type != arg1) 
            {
                this._type = arg1;
            }
            this.getDices();
            var loc1=this.getRondomPoints();
            var loc2=0;
            while (loc2 < this.diceAry.length) 
            {
                this._dice = this.diceAry[loc2] as DiceItem;
                if (arg1 != DiceRuningType.AllRun) 
                {
                    if (arg1 != DiceRuningType.FuNoRun) 
                    {
                        if (arg1 == DiceRuningType.ShowResult) 
                        {
                            this._dice.targetPoint = loc1[loc2];
                            this._dice.x = this._dice.targetPoint.x;
                            this._dice.y = this._dice.targetPoint.y;
                            loc4 = ((loc3 = this)._diceEventNum + 1);
                            loc3._diceEventNum = loc4;
                            this._dice.endPlayFunction = this.diceEndPlayFunction;
                            this._dice.setState(this._diceData[loc2]);
                            this._dice.start(-1, false);
                        }
                    }
                    else if (this._dice.isOk == false) 
                    {
                        loc4 = ((loc3 = this)._diceEventNum + 1);
                        loc3._diceEventNum = loc4;
                        this._dice.endPlayFunction = this.diceEndPlayFunction;
                        this._dice.setState(this._diceData[loc2]);
                        this._dice.start(-1);
                    }
                }
                else 
                {
                    this._dice.targetPoint = loc1[loc2];
                    this._dice.x = this._dice.targetPoint.x;
                    this._dice.y = this.getDiceDropDownFromY(loc2);
                    var loc3;
                    var loc4=((loc3 = this)._diceEventNum + 1);
                    loc3._diceEventNum = loc4;
                    this._dice.endPlayFunction = this.diceEndPlayFunction;
                    this._dice.setState(this._diceData[loc2]);
                    this._dice.start(this.power);
                }
                if (this._dice.parent == null) 
                {
                    this._diceSprite.addChild(this._dice);
                }
                ++loc2;
            }
            return;
        }

        public startPlay(arg1: int=-1): void
        {
            if (!this._hasInit) 
            {
                this.toPlayResult = arg1;
                return;
            }
            if (this._isRunning == true) 
            {
                return;
            }
            if (this._diceData == null || this._diceData.length == 0) 
            {
                this.clearDice();
                this._isRunning = false;
                this.callPlayEndFunction();
                return;
            }
            this._isRunning = true;
            this.rondomDice(arg1);
            return;
        }

        protected callPlayEndFunction(): void
        {
            if (typeof this._endPlayFunction === "function") 
            {
                this._endPlayFunction();
            }
            return;
        }

        protected diceEndPlayFunction(arg1: DiceItem): void
        {
            var loc1;
            var loc2=((loc1 = this)._diceEventNum - 1);
            loc1._diceEventNum = loc2;
            arg1.endPlayFunction = null;
            if (this._diceEventNum <= 0) 
            {
                this._diceEventNum = 0;
                this._isRunning = false;
                this.callPlayEndFunction();
            }
            return;
        }

        public show(): void
        {
            this.isShowing = true;
            if (!this._hasInit) 
            {
                return;
            }
            this._diceSprite.visible = true;
            if (this._moveingArea) 
            {
                this._moveingArea.visible = true;
            }
            return;
        }

        public hide(arg1: Boolean): void
        {
            this.isShowing = false;
            if (!this._hasInit) 
            {
                return;
            }
            if (this._moveingArea) 
            {
                this._moveingArea.visible = false;
            }
            this._diceSprite.visible = arg1;
            return;
        }

        public get hasInit(): Boolean
        {
            return this._hasInit;
        }

        public initView(arg1: Class, arg2: int, arg3: IDiceBar=null, arg4: IDiceHand=null, arg5: Object /* flash.display.DisplayObject */=null, arg6: Object /* flash.display.DisplayObject */=null, arg7: Object /* flash.display.DisplayObject */=null): void
        {
            if (this._hasInit) 
            {
                return;
            }
            this._hasInit = true;
            this._diceNum = arg2;
            this._powerbar = arg3;
            this._diceHand = arg4;
            this._animateClass = arg1;
            this._moveingArea = arg5;
            this._diceBg = arg6;
            if (this._diceBg) 
            {
                this.addChild(this._diceBg);
            }
            if (this._moveingArea) 
            {
                this.addChild(this._moveingArea);
                this._moveingArea.alpha = 0;
            }
            this._diceSprite = new Object /* flash.display.Sprite */();
            this.addChild(this._diceSprite);
            var loc1;
            this._diceSprite.mouseEnabled = loc1 = false;
            this._diceSprite.mouseChildren = loc1;
            if (arg7) 
            {
                this._disMaskPlayObject = arg7;
                this.addChild(arg7);
                this._diceSprite.mask = arg7;
            }
            this.power = 0;
            this._timer = new Object /* flash.utils.Timer */(30);
            if (this.stage) 
            {
                this.onAddedToStageHandler(null);
            }
            else 
            {
                this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            if (this.isShowing) 
            {
                this.show();
            }
            if (this.toPlayResult != -1000) 
            {
                this.startPlay(this.toPlayResult);
            }
            this.dispatchEvent(new Object /* flash.events.Event */("#finishInit#"));
            return;
        }

        public get isRunning(): Boolean
        {
            return this._isRunning;
        }

        public set endPlayFunction(arg1: Function)
        {
            this._endPlayFunction = arg1;
            return;
        }

        public set startPlayFunction(arg1: Function)
        {
            this._startPlayFunction = arg1;
            return;
        }

        protected getDices(): Array<any>
        {
            var loc1=null;
            var loc2=0;
            if (this.diceAry == null) 
            {
                this.diceAry = [];
                loc2 = 0;
                while (loc2 < this._diceNum) 
                {
                    loc1 = new DiceItem(this._animateClass);
                    this.diceAry.push(loc1);
                    this._diceSprite.addChild(loc1);
                    ++loc2;
                }
                this._diceItemW = loc1.width;
                this._diceItemH = loc1.height;
            }
            return this.diceAry;
        }

        public /* const */DefaultDiceData: Array<any>=[1, 1, 1, 1, 1, 1];

        protected /* var */diceAry: Array<any>;

        protected /* var */_disMaskPlayObject: Object /* flash.display.DisplayObject */;

        protected /* var */_powerbar: IDiceBar;

        protected /* var */_moveingArea: Object /* flash.display.DisplayObject */;

        protected /* var */_diceHand: IDiceHand;

        protected /* var */_diceBg: Object /* flash.display.DisplayObject */;

        protected /* var */_diceItemW: int;

        protected /* var */_diceItemH: int;

        protected /* var */_diceNum: int=0;

        /* internal */ /* var */_power: int=100;

        protected /* var */_diceSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_diceData: Array<any>;

        /* internal */ /* var */_tempPoints: Array<any>;

        /* internal */ /* var */_timer: Object /* flash.utils.Timer */;

        /* internal */ /* var */_isRunning: Boolean=false;

        /* internal */ /* var */_animateClass: Class;

        /* internal */ /* var */_hasInit: Boolean=false;

        public /* var */isShowing: Boolean=false;

        /* internal */ /* var */toPlayResult: int=-1000;

        /* internal */ /* var */_dice: DiceItem;

        /* internal */ /* var */_diceEventNum: int=0;

        /* internal */ /* var */_isShowHand: Boolean=false;

        /* internal */ /* var */_type: int=-1;

        /* internal */ /* var */_endPlayFunction: Function;

        /* internal */ /* var */isUp: Boolean;

        /* internal */ /* var */_startPlayFunction: Function;
    }
